09 December 2020

About this Document

This is an R Markdown presentation and it is created using Rstudio.

RStudio is an integrated development environment (IDE) for R. RStudio is available in open source and commercial editions and runs on the desktop (Windows, Mac, and Linux).

R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. R is available as Free Software.

Steps

  • Determine the libraries to use
  • Read the file and transform the data
  • Select the more appropriate kind of chart to represent the information
  • Create a dynamic time series chart

Libraries Used

R packages are a collection of R functions, compiled code and sample data. They are stored under a directory called “library” in the R environment. By default, R installs a set of packages during installation. More packages are added later when they are needed for some specific purpose.

library(readxl) #Read the data from excel sheet
library(DT) #Show a dynamic table
library(xts) #Create Time Series
library(highcharter) #Represent the data in timeseries chart
library(imager) #Open image

Load the Table and any other Changes in Data

#Load the table, establishing the sheet, range and column types of the data

Turtle <- read_excel("Turtles.xlsx", 
                     sheet = "Chart", range = cell_cols("A:C"), 
                     col_types = c("date","numeric", "numeric")) 

#Change column names

names(Turtle)[2] <- "egglaying"
names(Turtle)[3] <- "nharv"

Create a visualization of the Table

table <- datatable(Turtle, extensions = c('Buttons','FixedColumns', 'RowReorder'),
  filter = list(position = 'top', clear = FALSE),
  options = list(dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'), dom = 't',
    scrollX = TRUE,
    fixedColumns = TRUE,
    rowReorder = TRUE, order = list(c(0 , 'asc'))))

Consult the Table

Create the Time Series Dynamic Chart

A time series chart, also called a times series graph or time series plot, is a data visualization tool that illustrates data points at successive intervals of time. Each point on the chart corresponds to both a time and a quantity that is being measured.

# Change dataset to Time Series format

Turtle.ts.eggs <- xts(Turtle$egglaying, Turtle$Date)
Turtle.ts.nest <- xts(Turtle$nharv, Turtle$Date)
#Create the Time Series Chart

Chart <- highchart(type = "stock") %>% 
  hc_title(text = "Total number of Egg Laying Females and Percent of Nests Harvested
       <br>Ostional - Costa Rica, Period 2006-2010</br>") %>% 
  hc_subtitle(text = "Figure Adapted from: Valverde et al. 2012") %>% 
  hc_yAxis_multiples(list(title = list(text = "Egg Laying Females (total number)"), opposite = FALSE),
      list(showLastLabel = FALSE, opposite = TRUE, title = list(text = "Nests Harvested (percent)"))) %>% 
  hc_add_series(Turtle.ts.eggs,  yAxis = 0, type = "column", 
      pointInterval= 30*24*3600*1000, name="Egg Laying Females", color = "#2F528F")%>%
  hc_add_series(Turtle.ts.nest,  yAxis = 1, type = "spline", 
      pointInterval= 30*24*3600*1000, name="Percentage of Nests Harvested", color = "#ED7D31")%>%
  hc_plotOptions(spline = list(marker = list(enabled = TRUE, radius = 4))) %>% 
  hc_xAxis(title= list(text='Date'), type='datetime')

TimeSeries Dynamic Chart

Figure Adapted from: Valverde et al. 2012

im <- load.image("chart2.jpg")
im %>% plot(axes=F)